package ops.util;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import ops.WindowManager;
import ops.i18n.Resources;

public class ViewHelper {

	public static JScrollPane makeScrollPane(JComponent component) {
		JScrollPane scrollPane = new JScrollPane(component);
		
		scrollPane.setAutoscrolls(true);
		
		return scrollPane;
	}
	
	/**
	 * Simply method to create a default 'save' button.
	 * Will make our lifes easier if we want to add an Icon to the button. 
	 */
	public static JButton makeSaveButton() {
		Resources resources = Resources.getInstance();
		
		return new JButton(resources.getString("save"));
	}
	
	/**
	 * Simply method to create a default 'remove' button.
	 * Will make our lifes easier if we want to add an Icon to the button. 
	 */
	public static JButton makeRemoveButton() {
		Resources resources = Resources.getInstance();
		
		JButton button = new JButton(resources.getString("remove")); 
		button.setMnemonic('R');
				
		return button;
	}
	
	/**
	 * Simply method to create a default 'new' button.
	 * Will make our lifes easier if we want to add an Icon to the button. 
	 */
	public static JButton makeNewButton() {
		Resources resources = Resources.getInstance();
		
		return new JButton(resources.getString("new"));
	}
	
	/**
	 * Simply method to create a default 'cancel' button.
	 * Will make our lifes easier if we want to add an Icon to the button. 
	 */
	public static JButton makeCancelButton() {
		Resources resources = Resources.getInstance();
		
		return new JButton(resources.getString("cancel"));
	}
	
	/**
	 * Simply method to create a default 'add' button.
	 * Will make our lifes easier if we want to add an Icon to the button. 
	 */
	public static JButton makeAddButton() {
		Resources resources = Resources.getInstance();
		
		return new JButton(resources.getString("add"));
	}
	
	/**
	 * Shows a message box contating the exception's message.
	 * @param e
	 */
	public static void showError(Exception e) {
		showError(e.getLocalizedMessage());
	}

	/**
	 * Shows a message box with 'msg' as its content and container as its parent.
	 * @param msg
	 */
	public static void showError(final String msg) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JOptionPane.showMessageDialog(WindowManager.getInstance().getApplicationFrame(), msg, "Erro!", JOptionPane.ERROR_MESSAGE);
			}
		});
	}
	
	public static void showFormValidationError() {
		Resources resources = Resources.getInstance();
		
		ViewHelper.showError(resources.getString("form.validation.error"));
	}
}